const rootPath  = '../../../';
var request     = require(rootPath + 'common/request.js');
var common      = require(rootPath + 'common/common.js');
var options     = {
    data : {
        product_id      : 0,
        currentPrice    : 0,
        product         : null,             // 当前产品
        addMoney        : 1,                // 初始化加价 默认为一块钱
        startTimeSecond : 1488383999,       // 剩余时间
        dayLeft         : 0,                // 剩余天数
        hourLeft        : 0,                // 剩余小时
        minuteLeft      : 0,                // 剩余分钟
        seondLeft       : 0,                // 剩余秒数
        indicatorDots   : true,
        interval        : 5000,
        duration        : 1000,
        auctionCurrPage : 0,
        auctionTotalPage : 0,
    },

    onLoad : function (options) {
        var that = this;
        that.setData({
            product_id : options.id,
        });


    },

    onShow : function () {
        let that = this;
        let product_id = that.data.product_id;

        request.getProduct(product_id).then(response => {
            if (response.data.code === 'success') {
                that.setData({
                    product      : response.data.data,
                    currentPrice : response.data.data.current_price,
                });

                that.countdownStart();
            }
        }).catch(e => {
            console.log(e);
        });

        request.getAuctionList(product_id).then(response => {
            if (response.data.code === 'success') {
                console.log(response);
                that.setData({
                    auctions : response.data.data,
                });
            }
        }).catch(e => {
            console.log(e);
        });
    },

    // 减少价格
    tapMinusAddMoney : function () {
        if (this.data.addMoney === 1) {
            return;
        }

        this.setData({
            addMoney : --this.data.addMoney,
        });
    },

    // 点击增加价格
    tapPlusAddMoney : function () {
        this.setData({
            addMoney : ++this.data.addMoney,
        });
    },

    // 点击添加关注
    tapAttention : function () {
        let that    = this;
        let id      = that.data.product.id;

        request.attentionProduct(id).then(response => {
            console.log(response);
        }).catch(e => {
            console.log(e);
        });
    },

    // 输入加价内容
    inputAddMoney : function (e) {
        let value   = parseInt(e.detail.value);
        value       = (value >= 1) ? value : 1;

        this.setData({
            addMoney : value,
        });
    },

    // 加载竞价记录
    tapLoadAuction : function (e) {
        console.log(e);
        console.log('加载竞价记录');
    },

    auctionSubmit : function (e) {
        let that    = this;
        let data    = that.data;
        let reqData = {
            // form_id    : e.detail.formId,
            form_id    : 'aaaddd',
            product_id : that.data.product.id,
            add_price  : that.data.addMoney,
        };

        request.postAuctionProduct(reqData).then(response => {
            if (response.data.code === 'success') {
                that.setData({
                    currentPrice : response.data.data.cur_price,
                });
                wx.showToast({
                    title       : '加价成功!',
                    icon        : 'success',
                    duration    : 600,
                });
            }
            console.log(response);
        });

        console.log(reqData);
    },

    // 点击加价
    tapAddMoney : function (e) {
        let that = this;
        let data = {
            'add_price'  : '',
            'product_id' : '',
            'form_id'    : '',
        };
        console.log(that.data);
        console.log('加价');
    },

    // 开始倒计时
    countdownStart : function() {
        setInterval(() => {
            let [day, hour, minute, second] = common.timeLeft(this.data.startTimeSecond);
            this.setData({
                dayLeft     : day,
                hourLeft    : hour,
                minuteLeft  : minute,
                seondLeft   : second,
            });
        }, 1000);
    },
};

Page(options);
